package org.example.buffer;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 文件映射
 */
public class Demo4 {
    public static void main(String[] args) throws IOException {
        //开启内存文件
        try (RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw")) {
            //获取文件通道
            FileChannel channel = randomAccessFile.getChannel();
            //获取映射缓冲区
            MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
            //写入缓冲区
            mappedByteBuffer.put(0, (byte) '5');
            mappedByteBuffer.put(4, (byte) '5');
        }
    }
}
